<template>
  <div>
    <el-card class="dessqw" shadow="never">
      <div class="wdsnwksker">
        <!-- <div>
            <el-input v-model="phone" placeholder="请输入记账标题"></el-input>
          </div> -->
        <!-- <div>
            <el-input
              v-model="registeredUsername"
              placeholder="请输入分类名"
            ></el-input>
          </div> -->

        <!-- <div>
            <el-button type="primary" icon="el-icon-search">搜索</el-button>
          </div> -->
        <div>
          <el-button type="success" icon="el-icon-plus" @click="dshwnjs"
            >新增商品分类</el-button
          >
        </div>
      </div>
      <el-table
       
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }"
        :data="tableData"
        border
        style="width: 45%"
      >
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column prop="name" label="分类名称"> </el-table-column>
          <el-table-column prop="sort" label="排序"> </el-table-column>
        <el-table-column
          
          prop="updateTime"
          label="更新时间"
          width="180"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="wdsdw">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 15, 20]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog
      @close="close"
      :title="information + '商品分类名称'"
      :visible.sync="dialogVisible"
      width="25%"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <div class="wdssdw">
          <el-form-item label="分类名称" width="50" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
           <el-form-item label="排序" width="50" >
            <el-input v-model="form.sort" placeholder="排序大小（不填写则按照时间排序）"></el-input>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button type="success" @click="onSubmit('form')"
            >立即创建</el-button
          >
          <el-button @click="wdkswk">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Classify",
  data() {
    return {
      orders: "desc",
      sortField: "updateTime",
      total: 1,
      page: "1",
      pageSize: "10",
      dialogVisible: false,
      information: "",
      idsex: "",
      form: {
        name: "",
        sort:"",
      },
      currentPage4: 1,
      tableData: [],
      rules: {
        name: [
          { required: true, message: "请选择分类名称！", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.departurEvent();
  },
  methods: {
    sortTableFun(column) {
      //用户点击这一列的上下排序按钮时，触发的函数
      this.sortField = column.prop; //该方法获取到当前列绑定的prop字段名赋值给一个变量，之后这个变量做为入参传给后端
      if (column.prop) {
        //该列有绑定prop字段走这个分支
        if (column.order == "ascending") {
          //当用户点击的是升序按钮，即ascending时
          this.orders = "asc"; //将order这个变量赋值为后端接口文档定义的升序的字段名，之后作为入参传给后端
        } else if (column.order == "descending") {
          //当用户点击的是升序按钮，即descending时
          this.orders = "desc"; //将order这个变量赋值为后端接口文档定义的降序的字段名，之后作为入参传给后端
        }
        this.departurEvent();
      }
    },
    async departurEvent() {
      let params = {
        page: this.page,
        pageSize: this.pageSize,
        // sortField: this.sortField,
        // positiveOrReverse: this.orders,
      };
      let res = await this.$API.getCategoryList(params);
      if (res.code === 1) {
        const { data } = res;
        this.total = Number(data.total);
        this.tableData = data.records;
      }
      // console.log(res);
    },
    handleEdit(index, row) {
      this.idsex = "1";
      if (this.idsex) {
        this.information = "编辑";
      }
      // console.log(index, row);
      this.dialogVisible = !this.dialogVisible;
      this.form = { ...row };
    },
    handleDelete(index, row) {
      // console.log(index, row);
      this.$confirm(
        `此操作将永久删除该【${row.name}】分类名称吗？, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          let res = await this.$API.deleteCategory(row.id);
          // console.log(res);
          this.departurEvent();
          this.$message({
            type: "success",
            message: `删除【${row.name}】分类名称成功!`,
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (!this.idsex) {
            console.log("新增");
            console.log(this.form);
            // let category = {
            //   name: this.form.name,
            // };
            let res = await this.$API.addCategory(this.form);
            // console.log(res);
            this.dialogVisible = !this.dialogVisible;
            this.departurEvent();
            this.$message({
              type: "success",
              message: `新增分类名称成功!`,
            });
          } else {
            let res = await this.$API.updateCategory(this.form);
            // console.log(res);
            this.dialogVisible = !this.dialogVisible;
            this.departurEvent();
            this.form = {
              name: "",
            };
            // console.log("编辑");
            this.$message({
              type: "success",
              message: `编辑分类成功！`,
            });
          }
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    wdkswk() {
      this.dialogVisible = !this.dialogVisible;
      this.form = {
        name: "",
      };
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.departurEvent();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
      this.departurEvent();
    },
    dshwnjs() {
      if (!this.idsex) {
        this.information = "新增";
      }
      this.form = {
        name: "",
      };
      this.dialogVisible = !this.dialogVisible;
    },
    close() {
      this.idsex = "";
    },
  },
};
</script>

<style lang="scss" scoped>
.dessqw {
  width: 100%;
  padding: 0 20px;
}
.wdssdw {
  width: 350px;
}
.wdsdw {
  margin: 20px auto;
}
.wdsnwksker {
  display: flex;

  margin: 20px 0;
  width: 700px;
}
</style>